package com.rpay.admin.controller.news;

import com.rpay.admin.param.PostNews;
import com.rpay.admin.param.UpdateNews;
import com.rpay.common.dto.news.NewsDTO;
import com.rpay.common.dto.news.SqlQueryDTO;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.news.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/14
 * @Description:
 */
@RestController
@RequestMapping(value = "api/news")
@Validated
public class NewsController {
    @Autowired
    private INewsService iNewsService;

    @GetMapping(value = "")
    public PcsResult list(
            @RequestParam(name = "type", defaultValue = "") String newsType,
            @RequestParam(name = "keyword", defaultValue = "") String keyword,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        SqlQueryDTO query = new SqlQueryDTO();
        query.setNewsType(newsType);
        query.setKeyword(keyword);
        query.setPage((page - 1) * pageSize);
        query.setPageSize(pageSize);
        SqlQueryDTO data = iNewsService.getDataList(query, page, pageSize);
        return PcsResult.build(data);
    }

    @PostMapping(value = "")
    public PcsResult add(@Validated @RequestBody PostNews postNews) {
        NewsDTO news = new NewsDTO();
        news.setTitle(postNews.getTitle());
        news.setEnTitle(postNews.getEnTitle());
        news.setContent(postNews.getContent());
        news.setEnContent(postNews.getEnContent());
        news.setCover(postNews.getCover());
        news.setEnCover(postNews.getEnCover());
        news.setSubtitle(postNews.getSubtitle());
        news.setEnSubtitle(postNews.getEnSubtitle());
        news.setSource(postNews.getSource());
        news.setEnSource(postNews.getEnSource());
        news.setNewsType(postNews.getNewsType());
        iNewsService.saveNews(news);
        return new PcsResult();
    }

    @GetMapping(value = "/detail/{id}")
    public PcsResult detail(@PathVariable("id") Integer id) {
        NewsDTO query = new NewsDTO();
        query.setId(id);
        query.setVisible(null);
        NewsDTO newsDetail = iNewsService.getDetail(query);
        return PcsResult.build(newsDetail);
    }

    @PatchMapping(value = "/detail/{id}")
    public PcsResult update(@Validated @RequestBody UpdateNews updateNews, @PathVariable("id") Integer id) {
        NewsDTO news = new NewsDTO();
        news.setId(id);
        news.setVisible(updateNews.getVisible());
        news.setTitle(updateNews.getTitle());
        news.setEnTitle(updateNews.getEnTitle());
        news.setContent(updateNews.getContent());
        news.setEnContent(updateNews.getEnContent());
        news.setCover(updateNews.getCover());
        news.setEnCover(updateNews.getEnCover());
        news.setSubtitle(updateNews.getSubtitle());
        news.setEnSubtitle(updateNews.getEnSubtitle());
        news.setSource(updateNews.getSource());
        news.setEnSource(updateNews.getEnSource());
        news.setNewsType(updateNews.getNewsType());
        iNewsService.updateNews(news);
        return new PcsResult();
    }

    @DeleteMapping(value = "/detail/{id}")
    public PcsResult delete(@PathVariable Integer id) {
        iNewsService.deleteNews(id);
        return new PcsResult();
    }
}
